package com.wcpdoc.exam.core.dao.impl;

import org.springframework.stereotype.Repository;

import com.wcpdoc.core.dao.impl.RBaseDaoImpl;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.util.SqlUtil;
import com.wcpdoc.core.util.SqlUtil.Order;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.dao.QuestionTypeDao;
import com.wcpdoc.exam.core.entity.QuestionType;

import java.util.List;
import java.util.Map;

/**
 * 题库数据访问层实现
 *
 * v1.0 zhanghc 2016-5-24下午14:54:09
 */
@Repository
public class QuestionTypeDaoImpl extends RBaseDaoImpl<QuestionType> implements QuestionTypeDao {
	@Override
	public PageOut getListpage(PageIn pageIn) {
		String sql = "SELECT QUESTION_TYPE.ID, QUESTION_TYPE.NAME, CREATE_USER.NAME AS CREATE_USER_NAME, "
				+ "(" +
				"select group_concat(type,'（',ct,'道）') from (\n" +
				"SELECT QUESTION_TYPE_ID,count(*) as ct,case when type=1 then '单选' \n" +
				"\t\t\twhen type=2 then '多选' \n" +
				"\t\t\twhen type=3 then '填空' \n" +
				"\t\t\twhen type=4 then '判断' \n" +
				"\t\t\twhen type=5 then '问答' else '未知' end as type  \n" +
				"\tFROM EXM_QUESTION Z WHERE Z.STATE = 1  group by type,QUESTION_TYPE_ID \n" +
				") tb WHERE tb.QUESTION_TYPE_ID = QUESTION_TYPE.ID ) AS QUESTION_NUM "
				+ "FROM EXM_QUESTION_TYPE QUESTION_TYPE "
				+ "LEFT JOIN SYS_USER CREATE_USER ON QUESTION_TYPE.CREATE_USER_ID = CREATE_USER.ID ";
		SqlUtil sqlUtil = new SqlUtil(sql);
		sqlUtil.addWhere(ValidateUtil.isValid(pageIn.get("id")), "QUESTION_TYPE.ID = :ID", pageIn.get("id"))// 组卷回显规则的时候用
				.addWhere(ValidateUtil.isValid(pageIn.get("name")), "QUESTION_TYPE.NAME LIKE :NAME", String.format("%%%s%%", pageIn.get("name")))
				.addWhere(ValidateUtil.isValid(pageIn.get("curUserId", Integer.class)), "QUESTION_TYPE.CREATE_USER_ID = :CREATE_USER_ID", pageIn.get("curUserId", Integer.class))
				.addOrder("QUESTION_TYPE.ID", Order.DESC);
		return getListpage(sqlUtil, pageIn);
	}

	@Override
	public List<Map<String, Object>> getListUser(String id) {
		return null;
	}
}
